//
//  QuotationCenter.m
//  Stock
//
//  Created by blacksun on 11-7-4.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "QuotationCenter.h"
#import "QuotationStock.h"


@implementation QuotationCenter


-(void)queryMarketBrief{
    NSString *urlStr = [[NSString alloc] initWithFormat:@"%@jf/market/brief.jf",kHostName];
    NSLog(@"%@",urlStr);
    NSURL *url = [[NSURL alloc] initWithString:urlStr]; 
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:kQuotationCenterServiceMarketBrief,kASIHttpRequestUserinfoKey,nil];
    [request startAsynchronous];
    [self.workingRequests addObject:request];
    [request release];
    [url release];
    [urlStr release];
}

-(void)queryLevel1DataWithUri:(NSString *)requestUri{
    NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@%@",kHostName,requestUri]]; 
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:kQuotationCenterServiceLevel1,kASIHttpRequestUserinfoKey,nil];
    [request startAsynchronous];
    [self.workingRequests addObject:request];
    [request release];
    [url release];
}

-(void)queryQuotationStocksWithUri:(NSString *)requestUri{
    NSLog(@"%@",requestUri);
    NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@%@",kHostName,requestUri]]; 
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:kQuotationCenterServiceQueryQuotationStock,kASIHttpRequestUserinfoKey,nil];
    [request startAsynchronous];
    [self.workingRequests addObject:request];
    [request release];
    [url release];
}

#pragma mark -delegate callback
- (void)requestFinished:(ASIHTTPRequest *)request{
    NSDictionary *userinfo = request.userInfo;
    CommonUtil *commonUtil = [CommonUtil shareInstance];
    NSString *action = [userinfo objectForKey:kASIHttpRequestUserinfoKey];
    if ([kQuotationCenterServiceMarketBrief isEqualToString:action]) {
        NSString *responseString = [request responseString];
        NSArray *stockDicts = [[responseString JSONValue] objectForKey:@"stocks"];
        NSMutableArray *stocks = [NSMutableArray array];
        for (NSDictionary *stockDict in stockDicts) {
            MyStock *stock = [[MyStock alloc] init];
            [commonUtil copyMapEntries:stockDict toObject:stock];
            [stocks addObject:stock];
            [stock release];
        }
        [self.delegate requestFinishedWithResult:stocks andUserinfo:userinfo];
    }else if([kQuotationCenterServiceLevel1 isEqualToString:action]){
        NSString *responseString = [request responseString];
        NSArray *result = [[responseString JSONValue] objectForKey:@"sectors"];
        [self.delegate requestFinishedWithResult:result andUserinfo:userinfo];
    }else if([kQuotationCenterServiceQueryQuotationStock isEqualToString:action]){
        NSString *responseString = [request responseString];
        NSArray *result = [[responseString JSONValue] objectForKey:@"stocks"];
        NSMutableArray *quotationStocks = [NSMutableArray array];
        for (NSDictionary *dict in result) {
            QuotationStock *quotationStock = [[QuotationStock alloc] init];
            [commonUtil copyMapEntries:dict toObject:quotationStock];
            [quotationStocks addObject:quotationStock];
            [quotationStock release];
        }
        [self.delegate requestFinishedWithResult:quotationStocks andUserinfo:userinfo];
    }
}

- (void)requestFailed:(ASIHTTPRequest *)request{
    NSLog(@"request error");
    [self.delegate requestFinishedwithError:request.error andUserinfo:request.userInfo];
}
@end
